<?php
/**
 * Created by Engineer CuiLiwu.
 * Project: deal.
 * Date: 2018/6/22-16:34
 * License Hangzhou orce Technology Co., Ltd. Copyright © 2018
 */

namespace App\Http\Controllers\Web\Seller;

use App\ConstDir\ErrorConst;
use App\Repository\Models\Orders;
use App\Repository\Repositories\Interfaces\OrdersRepository;
use App\Http\Controllers\Web\BaseController;
use Illuminate\Http\Request;

class ServiceOrdersController extends BaseController
{
    protected $serviceOrders_repo;


    protected $request;
    protected $shop_id;

    public function __construct(Request $request,OrdersRepository $serviceOrders_repo)
    {
        parent::__construct();
        $this->request = $request;
        $this->shop_id = array_get($this->user, 'shop_id');
        $this->serviceOrders_repo = $serviceOrders_repo;

        //用户只能操作自身所属数据
        $this->serviceOrders_repo->scopeQuery(function ($query) {
            return $query->where('shop_id', $this->shop_id)->where('order_type',6);
        });
    }

    public function index()
    {
        $orders = $this->serviceOrders_repo->paginate($this->perPage);
        return $this->success(ErrorConst::SUCCESS_CODE_MSG, $orders, true);
    }

    public function show($id)
    {
        $order = $this->serviceOrders_repo->find($id);
        return $this->success(ErrorConst::SUCCESS_CODE_MSG, $order['data']);
    }

    public function store(Request $request)
    {
    }

    public function update(Request $request, $id)
    {
        $this->validate($request, [
            'order_status' => 'required',
            'cancel_reason' => 'integer|in:' . implode(',', array_keys(Orders::$cancel_reason_map))
        ]);
        $data = $request->all(['order_status', 'cancel_reason']);

        $ret = $this->serviceOrders_repo->update($data, $id);

        return $this->noDataReturn($ret ? true : false);
    }

    public function delete($id)
    {
        //用户无删除订单操作，以下作为样例
//        $ret = $this->serviceOrders_repo->delete($id);
//
//        return $this->noDataReturn($ret ? true : false);
    }
}